home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9492 < prev    next >
Encoding:
Text File  |  1996-08-05  |  9.0 KB  |  444 lines

  1. Path: lantana.singnet.com.sg!usenet
  2. From: Teddy Bear <s7700038@singnet.com.sg>
  3. Newsgroups: comp.lang.c
  4. Subject: Almost got it
  5. Date: 11 Mar 1996 14:46:16 GMT
  6. Organization: Singapore Telecom Internet Service
  7. Message-ID: <4i1ebo$96a@lantana.singnet.com.sg>
  8. NNTP-Posting-Host: ts900-3920.singnet.com.sg
  9. Mime-Version: 1.0
  10. Content-Type: multipart/mixed;
  11.     boundary="-------------------------------85592058322313"
  12. X-Mailer: Mozilla 1.22 (Windows; I; 16bit)
  13.  
  14. This is a multi-part message in MIME format.
  15.  
  16. ---------------------------------85592058322313
  17. Content-Transfer-Encoding: 7bit
  18. Content-Type: text/plain; charset=us-ascii
  19.  
  20. I've almost got the program right, but there seems to be some problem 
  21. with my count
  22. Can anyone help me debug and check?
  23. Thanks
  24. I need this ASAP.
  25. Thanks
  26.  
  27. ---------------------------------85592058322313
  28. Content-Transfer-Encoding: 7bit
  29. Content-Type: text/plain
  30.  
  31. #include <stdio.h>
  32. #include <conio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35.  
  36.  
  37. typedef struct node{
  38.     int age;               //individual's age
  39.     char name[40];         //individual's name
  40.     char address[40];     //individual's address
  41.     struct node *next;
  42. }node;
  43. node *head;
  44. typedef node* nodeptr;
  45.  
  46. void createlist(nodeptr *ptrtohead, nodeptr head);
  47. int  countlist(nodeptr head);
  48. nodeptr searchlist(nodeptr head, char *wanted);
  49. void editlist(nodeptr head);
  50. void displaylist(nodeptr head);
  51. void save_record(FILE *fileptr, nodeptr *head, int count);
  52. void load_record(FILE *fileptr, nodeptr *head, nodeptr *tail, int *count);
  53. nodeptr addlist(nodeptr head);
  54. nodeptr deletelist(nodeptr head);
  55. int main()
  56. {
  57.     char option;
  58.     nodeptr ptr, *ptrtohead, head, tail;
  59.     int count=0;
  60.     FILE *fileptr;
  61.     head = NULL;
  62.  
  63.     option = '1';
  64.     do
  65.     {
  66.     clrscr();
  67.     printf("\n\n\nThis program .....");
  68.     printf("\n\n1) Read existing list from disk\n");
  69.     printf("2) Add list\n");
  70.     printf("3) Delete list\n");
  71.     printf("4) Search list\n");
  72.     printf("5) Display List\n");
  73.     printf("6) Save list to disk\n");
  74.     printf("7) Kill Yi Yong !!! ( EXIT )\n");
  75.     printf("\nPlease enter your choice : ");
  76.  
  77.     if(option < '1' || option > '7')
  78.         printf("\nInvaild option. Please re-enter:");
  79.     option = getche();
  80.     switch(option)
  81.     {
  82.         case '1' : load_record(fileptr, &head, &tail, &count);
  83.                displaylist(head);
  84.                printf("\n\nPress any key to continue.. ");
  85.                getch();
  86.                break;
  87.         case '2' : if(head == 0)
  88.                {
  89.                createlist(ptrtohead, NULL);
  90.                head = *ptrtohead;
  91.                }
  92.                else
  93.                head = addlist(head);
  94.                printf("\n\nPress any key to continue.. ");
  95.                getch();
  96.                break;
  97.         case '3' : if (head == 0)
  98.             printf("nothing to delete");
  99.             head = deletelist(head);
  100.                printf("\n\nPress any key to continue.. ");
  101.                getch();
  102.                break;
  103.         case '4' : editlist(head);
  104.                printf("\n\nPress any key to continue.. ");
  105.                getch();
  106.                break;
  107.         case '5' : displaylist(head);
  108.                printf("\nPress any key to cont...");
  109.                getch();
  110.                break;
  111.         case '6' :   save_record(fileptr, &head, count);
  112.                displaylist(head);
  113.                break;
  114.         case '7' : clrscr();
  115.                exit(0);
  116.  
  117.     }
  118.     }
  119.     while(option != '7');
  120.     return 0;
  121. }
  122.  
  123. void load_record(FILE *fileptr, nodeptr *head, nodeptr *tail, int *count)
  124. //get list.dat from disk
  125. {
  126.      nodeptr load;
  127.      int node_size=sizeof(node);
  128.      int name_length,address_length;
  129.  
  130.      if((fileptr=fopen("b:list.dat","rb"))==NULL)
  131.      {
  132.       printf("\a\n\nUnable to open file\n");
  133.       printf("\nPress any key to continue....");
  134.       getch();
  135.      }
  136.      else
  137.      {
  138.       fscanf(fileptr,"%d\n", &(*count));
  139.       while(!feof(fileptr))
  140.       {
  141.           load=(nodeptr)malloc(node_size);
  142.           fscanf(fileptr, "%d\n", &name_length);
  143.           fscanf(fileptr, "%d\n", &address_length);
  144.           fgets(load->name,name_length+1, fileptr);
  145.           fscanf(fileptr,"\n");
  146.           fgets(load->address, address_length+1, fileptr);
  147.           fscanf(fileptr, "%d\n",  &load->age);
  148.  
  149.           if((*head)==NULL)
  150.           {
  151.            (*head)=load;
  152.            (*head)=load;
  153.           }
  154.           else
  155.           {
  156.            (*tail)->next=load;
  157.            (*tail)=load;
  158.           }
  159.       }
  160.       (*tail)->next=NULL;
  161.      }
  162.      fclose(fileptr);
  163.      return;
  164. }
  165.  
  166.  
  167. void save_record(FILE *fileptr, nodeptr *head, int count)
  168. //save info to disk file list.dat
  169. {
  170.      nodeptr save;
  171.  
  172.      if ((fileptr=fopen("b:list.dat","wb"))==NULL)
  173.      {
  174.       printf("\a\n\nERROR!!!!Unable to open file!!!\n");
  175.      }
  176.      else
  177.      {
  178.       save=(*head);
  179.       fprintf(fileptr,"%d\n",count);
  180.       while(save!=NULL)
  181.       {
  182.            fprintf( fileptr,"%d\n",  strlen(save->name));
  183.            fprintf( fileptr, "%d\n", strlen(save->address));
  184.            fprintf( fileptr, "%s\n", save->name);
  185.            fprintf( fileptr, "%s\n", save->address);
  186.            fprintf( fileptr, "%d\n", save->age);
  187.            save=save->next;
  188.       }
  189.      }
  190.      fclose(fileptr);
  191.      printf("\a\n\nSaving Done!!!!!\n");
  192.      printf("\nPress any key to continue.....");
  193.      return;
  194. }
  195.  
  196.  
  197.  
  198. void createlist(nodeptr *ptrtohead, nodeptr head)
  199. //create a link list
  200. {
  201.     nodeptr temp, last;
  202.     char ans;
  203.     int size = sizeof(node);
  204.     do
  205.     {
  206.     temp=(nodeptr)malloc(size);
  207.     clrscr();
  208.     printf("\nEnter Name    : ");
  209.     gets(temp->name);
  210.     printf("Enter Age     : ");
  211.     scanf("%d",&temp->age);
  212.     fflush(stdin);
  213.     printf("Enter Address : ");
  214.     gets(temp->address);
  215.  
  216.     if(head == NULL)
  217.     {
  218.         head = temp;
  219.         last = temp;
  220.     }
  221.     else
  222.     {
  223.         last->next=temp;
  224.         last = temp;
  225.     }
  226.     printf("\nWould u like to enter more ? <Y/N> : ");
  227.     ans=toupper(getch());
  228.     }
  229.     while(ans != 'N');
  230.     last->next = NULL;
  231.     *ptrtohead = head;
  232. }
  233.  
  234. nodeptr searchlist(nodeptr head, char *want)
  235.  
  236. {
  237.     int element, count, found, condition;
  238.     nodeptr ptr;
  239.     element = countlist(head);
  240.     ptr = head;
  241.     found = 0;
  242.     condition = 1;
  243.     for(count=0; (count<element)&&(condition); )
  244.     {
  245.     if((strcmp(ptr->name, want))==0)
  246.     {
  247.         found = 1;
  248.         condition = 0;
  249.     }
  250.     else
  251.         if((strcmp(ptr->name, want)) > 0)
  252.         condition = 0;
  253.         else
  254.         {
  255.         ptr = ptr->next;
  256.         count++;
  257.         }
  258.     }
  259.  
  260.     if(found)
  261.     return ptr;
  262.     else
  263.     return NULL;
  264. }
  265.  
  266. int countlist(nodeptr head)
  267. //count no of individuals
  268. {
  269.     int count;
  270.     nodeptr ptr;
  271.     ptr = head;
  272.     if(ptr != NULL)
  273.     {
  274.     for(count=1;ptr->next!=NULL; count++)
  275.     ptr = ptr->next;
  276.     return count;
  277.     }
  278.     else
  279.     {
  280.     return 0;
  281.     }
  282. }
  283.  
  284. void  editlist(nodeptr head)
  285. {
  286.  
  287.     char *find, answer='Y';
  288.     int cnt, check;
  289.     nodeptr ptrtoelm;
  290.     do
  291.     {
  292.     clrscr();
  293.     printf("\n\nEnter Name   :  ");
  294.     gets(find);
  295.     ptrtoelm = searchlist(head, find);
  296.     if(ptrtoelm==NULL)
  297.     {
  298.         printf("\a\nSORRY! THERE IS NO SUCH NAME\n");
  299.         printf("\nDo you wish to continue searching? <Y/N> : ");
  300.         answer = toupper(getch());
  301.     }
  302.     if(answer == 'N')
  303.         return;
  304.    }while(ptrtoelm==NULL && answer == 'Y');
  305.  
  306.  
  307.     printf("\nName    : %s\n", ptrtoelm->name);
  308.     printf("Age     : %d\n", ptrtoelm->age);
  309.     printf("Address : %s\n", ptrtoelm->address);
  310. }
  311.  
  312.  
  313. void displaylist(nodeptr head)
  314. // Display the information in the link.
  315. {
  316.     nodeptr ptr;
  317.     ptr = head;
  318.     clrscr();
  319.     while(ptr!=NULL)
  320.     {
  321.     printf("\nNAME: %s \n",ptr->name);
  322.     printf("AGE: %d \n",ptr->age);
  323.     printf("ADDRESS: %s \n",ptr->address);
  324.     ptr = ptr->next;
  325.     }
  326.  
  327. }
  328.  
  329. nodeptr clearlist(nodeptr head)
  330.  
  331. {
  332.     nodeptr ptr;
  333.     while(head != NULL)
  334.     {
  335.     ptr = head;
  336.     head = head->next;
  337.     free(ptr);
  338.     }
  339.     free(ptr);
  340.     return head;
  341. }
  342.  
  343. nodeptr addlist(nodeptr head)
  344.  
  345. {
  346.  
  347.     nodeptr front, back, newitem;
  348.     int check, cnt;
  349.     newitem = (nodeptr)malloc(sizeof(node));
  350.     clrscr();
  351.     printf("\n\nEnter name to be added : ");
  352.     gets(newitem->name);
  353.     if((strcmp((back->name),(front->name))) == 0)
  354.     {
  355.     printf("\a");
  356.     printf("\n\nDUPLICATE COPY");
  357.     strcpy(back->name,front->name);
  358.     back->age = front->age;
  359.     strcpy(back->address,front->address);
  360.     getch();
  361.     }
  362.     fflush(stdin);
  363.     printf("Enter age              : ");
  364.     scanf("%d", &newitem->age);
  365.     fflush(stdin);
  366.     printf("Enter address          : ");
  367.     gets(newitem->address);
  368.     if((strcmp(newitem->name, head->name)) <= 0)
  369.     {
  370.     newitem->next = head;
  371.     head = newitem;
  372.     }
  373.     else
  374.     {
  375.     back = head;
  376.     front = back->next;
  377.     while(front!=NULL)
  378.     {
  379.         if((strcmp(newitem->name, front->name)) > 0)
  380.         {
  381.         back = front;
  382.         front = front->next;
  383.         }
  384.         else
  385.         break;
  386.     }
  387.     newitem->next = front;
  388.     back->next = newitem;
  389.     }
  390.     return head;
  391. }
  392.  
  393. nodeptr deletelist(nodeptr head)
  394.  
  395. {
  396.     nodeptr front, back;
  397.     char *tempname;
  398.     if(head != NULL)
  399.     {
  400.     back = head;
  401.     front = back->next;
  402.     }
  403.     else
  404.     {
  405.     printf("\n\aDelete not allowed.");
  406.     getch();
  407.     return NULL;
  408.     }
  409.     tempname = (char *)malloc(sizeof(char *));
  410.     clrscr();
  411.     printf("\n\nEnter name to be delete : ");
  412.     gets(tempname);
  413.     if((strcmp(tempname,back->name)) == 0)
  414.     {
  415.     front = head;
  416.     back = back->next;
  417.     free(front);
  418.     return back;
  419.     }
  420.     else
  421.     {
  422.     while(((strcmp(tempname, front->name))!=0) && (front!=NULL))
  423.     {
  424.         back = front;
  425.         front = front->next;
  426.     }
  427.     if(front==NULL)
  428.     {
  429.         printf("\n\aNo such person in the list.");
  430.         return head;
  431.     }
  432.     else
  433.     {
  434.         back->next = front->next;
  435.         free(front);
  436.         return head;
  437.     }
  438.     }
  439.  
  440. }
  441.  
  442.  
  443. ---------------------------------85592058322313--
  444.